<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<div id="addOrderContainer" class="modal fade" tabindex="-1" >
	<div class="modal-dialog" style="width: 425px; margin-top: 100px">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true"></button>
				<h4 class="modal-title">添加收件人</h4>
			</div>
			<div class="modal-body">
				<div data-always-visible="1" data-rail-visible1="1" style="height:auto;">
                    <div class="input-group" >
                    	<label class="col-md-3 control-label" style="width: 90px;" >电话号码：</label>
                    	<input id="orderPhone" class="form-control" style="width: 200px" onkeyup="inputKeyup(event)" maxlength="11" /> 
                    	<button type="button" onclick="addOrder('')" class="btn btn-default">添加</button>
                    </div>
				</div>
				<div style="height: 55px; ">
					<ul id="smsPanel">
						<s:iterator value="templates" status="smsTemplate">
						
						<li>
							
							<input type="radio" id="sms_<s:property value="entityId"/>" <s:if test="#smsTemplate.index == 0">checked="checked"</s:if> name="sms" value="<s:property value="entityId"/>">
							<label for="sms_<s:property value="entityId" />"><s:property value="name"/></label>
							<input type="hidden" id="content_<s:property value="entityId" />" value='<s:property value="content"/>' />
							<input type="hidden" id="sign_<s:property value="entityId" />" value='<s:property value="sign"/>' />
						</li>
						</s:iterator>
					</ul>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" onclick="closeAddOrder()" class="btn btn-default">关闭</button>
				<button type="button" onclick="addOrder('')" class="btn btn-success">创建</button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function() {  
	
	$('#addOrder').live('click', function (e) {
		
		$('#addOrderContainer').modal({
	        show: 'true'
	    });
		$("#orderPhone").focus();
		//document.onkeyup =inputKeyup;
		$('input[name="sms"]')[0].checked = true;
	});
	
});

function closeAddOrder() {
	$("#addOrderContainer .close").click();
	window.location.reload();
}

function inputKeyup(evt) {
	
	evt = (evt) ? evt : ((window.event) ? window.event : "");  
	var currKey=evt.keyCode||evt.which||evt.charCode;

	if(currKey == 13) {
		var phone = $("#orderPhone").val();
		$("#orderPhone").val("");
		addOrder(phone);
		return;
	}
}

function addOrder(phone) {
	if(phone == ""){
		phone = $("#orderPhone").val();
	}
	
	if(phone == "") {
		return;
	}
	
	if(phone.length != 11) {
		alert("请填写正确的手机号码!");
		$("#orderPhone").focus();
		return;
	}

	var sms = $('input[name="sms"]:checked').val();
	var content = $("#content_" + sms).val();
	var sign = $("#sign_" + sms).val();
	ajaxPost('<s:url action="save" namespace="/admin/order" />',
		{"orders.phone": phone, "orders.content" : content, "orders.sign" : sign},
		function(response){
			var status = response.status;
			if(status == 200) {
				$("#orderPhone").val("").focus();
			} else {
				alert("添加失败，请检查短信机是否过时或号码的正确性!");
			}
		}
	);
	
}
</script>